﻿<?php
class Developer
{
	private $email;
	private $company;
	private $password;
	private $tokcen;
	private $mustangDB;
	public function __construct($mustangDB){
		$this->mustangDB = $mustangDB;
	}
	
	public function initNew($email, $company, $password)
	{
		$this->email = $email;
		$this->company = $company;
		$this->password = MD5($password);
		$this->token = MD5($email . $company . $password . microtime ());
	}
	
	public static function exist($email)
	{
		global $mustangDB;
		$mustangDB->query("select * from t_developer where email = '" . 
			mysql_real_escape_string($email) .
			"'");
		return $mustangDB->count();
	}
	
	public static function existCompany($company)
	{
		$this->mustangDB->query("select * from t_developer where company = '" . 
			mysql_real_escape_string($company) .
			"'");
		return $this->mustangDB->count();
	}
	
	public function save(){
		$this->mustangDB->query("insert into t_developer (email, company, password, token, isactive) values('" . 
			mysql_real_escape_string($this->email) . 
			"','" . 
			mysql_real_escape_string($this->company) . 
			"','" . 
			mysql_real_escape_string($this->password) . 
			"','" . 
			mysql_real_escape_string($this->token) .
			"',0)");
		$this->mustangDB->query("select * from t_developer where email = '" . 
			mysql_real_escape_string($this->email) .
			"'");
		$row = $this->mustangDB->getRow();
		return $row['id'];
	}
	public function getToken(){
		return $this->token;
	}
	public function getEmail(){
		return $this->email;
	}
	
	public function initWithToken($token){
		$this->mustangDB->query("select * from t_developer where token = '" . mysql_real_escape_string($token) . "'");
		$result = $this->mustangDB->getRow();
		if($result){
			$this->initWithResult($result);
			return true;
		}else{
			return false;
		}
	}
	
	public function initWithEmailPassword($email, $password){
		$this->mustangDB->query("select * from t_developer where email = '" .
			mysql_real_escape_string($email) .
			"' and password = '" . 
			MD5(mysql_real_escape_string($password)) .
			"'");
		$result = $this->mustangDB->getRow();
		if($result){
			$this->initWithResult($result);
			return true;
		}else{
			return false;
		}
	}
	private function initWithResult($result){
		$this->id = $result["id"];
		$this->email = $result["email"];
		$this->company = $result["company"];
		$this->token = $result["token"];
	}
	
	public function activate(){
		$this->mustangDB->query("update t_developer set isactive = 1 where id = " . mysql_real_escape_string($this->id));
	}
	
	public function getInfo(){
		return new DeveloperInfo($this->id, $this->email, $this->company);
	}
}

class DeveloperInfo
{
	public $id;
	public $email;
	public $company;
	
	public function __construct($id, $email, $company){
		$this->id = $id;
		$this->email = $email;
		$this->company = $company;
	}
	
}
?>